package com.xiaozhaovip.demo.controller;

import com.xiaozhaovip.demo.Enum.YueQiLaiActivityJoinerStatusEnum;
import com.xiaozhaovip.demo.Enum.YueQiLaiActivityStatusEnum;
import com.xiaozhaovip.demo.intf.DO.ResponseDO;
import com.xiaozhaovip.demo.intf.entity.YueQiLaiActivityJoiner;
import com.xiaozhaovip.demo.intf.entity.YueQiLaiInfo;
import com.xiaozhaovip.demo.intf.service.YueQiLaiActivityJoinerService;
import com.xiaozhaovip.demo.intf.service.YueQiLaiInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Cauli
 */
@Slf4j
@RestController
@RequestMapping("/yueQiLai/activity/sign")
public class YueQiLaiActivityJoinerController extends BaseController {
    @Resource
    private YueQiLaiInfoService yueQiLaiInfoService;

    @Resource
    private YueQiLaiActivityJoinerService yueQiLaiActivityJoinerService;

    /**
     * 模拟当前登录用户
     */
    private final int currentUserId = 2;

    /**
     * 改变用户的报名状态，增加活动的报名数量
     *
     * @param activityId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/activitySignUp")
    public ResponseDO activitySignUp(int activityId) {
        // 参数验证
        if (currentUserId < 1) {
            return new ResponseDO(false, "请登录后再操作！", FAIL_CODE, currentUserId);
        }
        if (activityId < 1) {
            return new ResponseDO(false, "活动id错误！", FAIL_CODE, activityId);
        }
        log.info("signUp activity," + activityId, "signUp user," + currentUserId);
        // 获得活动详情对象
        YueQiLaiInfo activityInfo = yueQiLaiInfoService.loadById(activityId);
        if (activityInfo == null) {
            return new ResponseDO(true, "该活动不存在！", FAIL_CODE, activityId);
        } else if (activityInfo.getJoinNum() >= activityInfo.getMostJoinNum()) {    // 活动报名已满,不能报名
            return new ResponseDO(true, SUCCESS, SUCCESS_CODE, "活动报名已满！");
        } else if (activityInfo.getStatus() == YueQiLaiActivityStatusEnum.SignUpActivityClosed.getId()) {   //不在活动报名时间,不能报名
            return new ResponseDO(true, SUCCESS, SUCCESS_CODE, "不在活动报名时间！");
        }

        // 获得活动报名用户对象
        YueQiLaiActivityJoiner activityJoiner = yueQiLaiActivityJoinerService.loadByUserIdAndActivityId(currentUserId, activityId);
        if (activityJoiner != null) {  // 活动报名记录存在
            if (activityJoiner.getStatus() == YueQiLaiActivityJoinerStatusEnum.WaitingToStart.getId()) {    // 已报名该活动,不能报名
                return new ResponseDO(true, SUCCESS, SUCCESS_CODE, "你已报名该活动！");
            } else {    // 未报名该活动,可以报名
                // 修改记录
                yueQiLaiActivityJoinerService.updateJoinerStatusById(currentUserId, activityId, YueQiLaiActivityJoinerStatusEnum.WaitingToStart.getId());
            }
        } else {  // 活动报名记录不存在
            activityJoiner = new YueQiLaiActivityJoiner();
            activityJoiner.setActivityId(activityId);
            activityJoiner.setJoinerId(currentUserId);
            activityJoiner.setStatus(YueQiLaiActivityJoinerStatusEnum.WaitingToStart.getId());
            // 新增记录
            yueQiLaiActivityJoinerService.insert(activityJoiner);
        }
        // 报名人数增加
        yueQiLaiInfoService.incrJoinNumById(activityId);
        return new ResponseDO(true, SUCCESS, SUCCESS_CODE, "报名活动成功！");
    }

    /**
     * 改变用户的报名状态，减少活动的报名数量
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/activitySignDown")
    public ResponseDO activitySignDown(int activityId) {
        // 参数验证
        if (currentUserId < 1) {
            return new ResponseDO(false, "请登录后再操作", FAIL_CODE, currentUserId);
        }
        if (activityId < 1) {
            return new ResponseDO(false, "活动id错误", FAIL_CODE, activityId);
        }

        // 获得活动报名用户对象
        YueQiLaiActivityJoiner activityJoiner = yueQiLaiActivityJoinerService.loadByUserIdAndActivityId(currentUserId, activityId);

        if (activityJoiner == null
                || activityJoiner.getStatus() == YueQiLaiActivityJoinerStatusEnum.NotSignUpAndActivityEnd.getId()
                || activityJoiner.getStatus() == YueQiLaiActivityJoinerStatusEnum.SignedUpAndCancelled.getId()) {   // 空或者未报名状态
            return new ResponseDO(true, SUCCESS, SUCCESS_CODE, "你未参加该活动！");
        } else {    // 非空且已报名状态
            yueQiLaiActivityJoinerService.updateJoinerStatusById(currentUserId, activityId, YueQiLaiActivityJoinerStatusEnum.SignedUpAndCancelled.getId());
            yueQiLaiInfoService.decrJoinNumById(activityJoiner.getActivityId());
            return new ResponseDO(true, SUCCESS, SUCCESS_CODE, "取消报名活动成功！");
        }
    }
}
